package com.jinzhi.jzweb.controller;


import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.mapper.Wrapper;
import com.baomidou.mybatisplus.plugins.Page;
import com.jinzhi.common.annotation.Log;
import com.jinzhi.common.base.BaseController;
import com.jinzhi.common.config.Constant;
import com.jinzhi.common.utils.Result;
import com.jinzhi.jzweb.domain.ChapterDO;
import com.jinzhi.jzweb.domain.CoursesDO;
import com.jinzhi.jzweb.domain.KnowledgePointsDO;
import com.jinzhi.jzweb.service.ChapterService;
import com.jinzhi.jzweb.service.CoursesService;
import com.jinzhi.jzweb.service.KnowledgePointsService;
import org.apache.commons.lang3.StringUtils;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

/**
 *
 * <pre>
 * 章节表
 * </pre>
 * <small> 2020-02-24 21:00:11 | lance</small>
 */
@Controller
@RequestMapping("/jzweb/chapter")
public class ChapterController extends BaseController {
	@Autowired
	private ChapterService chapterService;
	@Autowired
	private CoursesService coursesService;
	@Autowired
	private KnowledgePointsService knowledgePointsService;

	@GetMapping()
	@RequiresPermissions("jzweb:chapter:chapter")
	String Chapter(String id,String parentId,Model model){
		model.addAttribute("coursesId",id);
		if(parentId!=null && !parentId.equals("")){
			model.addAttribute("parentId",parentId);
			System.out.println("parentId:---Chapter"+parentId);
		}
		System.out.println("coursesId:---Chapter"+id);

		return "jzweb/chapter/chapter";
	}

	@ResponseBody
	@GetMapping("/list")
	@RequiresPermissions("jzweb:chapter:chapter")
	public Result<Page<ChapterDO>> list(ChapterDO chapterDTO){
		if(chapterDTO.getId()!=null)
			System.out.println("id:"+chapterDTO.getId());
		Wrapper<ChapterDO> wrapper = new EntityWrapper<ChapterDO>().orderBy("id", false);
		if (chapterDTO.getState() != null){
			wrapper.eq("state",chapterDTO.getState());
		}
		if (chapterDTO.getCoursesId() != null){
			wrapper.eq("courses_id",chapterDTO.getCoursesId());
		}
		if (chapterDTO.getName() != null){
			wrapper.like("name",chapterDTO.getName());
		}
		if(chapterDTO.getParentId()!=null && !chapterDTO.getParentId().equals("")){
			wrapper.eq("parent_id",Long.valueOf(chapterDTO.getParentId()));
			wrapper.eq("is_root",0);
		}else {
			wrapper.eq("is_root",1);
		}

		if (StringUtils.isNotBlank(chapterDTO.getKpointsName())){
			Wrapper<KnowledgePointsDO> knWrapper = new EntityWrapper<KnowledgePointsDO>().like("name",chapterDTO.getKpointsName()).and().eq("pid",0);
			List<KnowledgePointsDO>  knowledgePointsDOS = knowledgePointsService.selectList(knWrapper);
			String str = "";
			for (KnowledgePointsDO knowledgePointsDO : knowledgePointsDOS){
				str = str + knowledgePointsDO.getId() + ",";
			}
			wrapper.and();
			if(StringUtils.isNotBlank(str)){
				wrapper.in("kpoints_id",str.substring(0,str.length()-1));
			}else{
				wrapper.eq("kpoints_id",null);
			}
		}

		Page<ChapterDO> page = chapterService.selectPage(getPage(ChapterDO.class), wrapper);
		List list = new ArrayList();
		for (int i = 0; i < page.getRecords().size(); i++) {
			ChapterDO obj = (ChapterDO) page.getRecords().get(i);
			if (obj.getCoursesId() != null){
				CoursesDO coursesDO = coursesService.selectById(obj.getCoursesId());
				if (coursesDO != null) {
					obj.setCoursesName(coursesDO.getName());
				}
			}
			if (obj.getKpointsId() != null){
				KnowledgePointsDO knowledgePointsDO = knowledgePointsService.selectById(obj.getKpointsId());
				if (knowledgePointsDO != null) {
					obj.setKpointsName(knowledgePointsDO.getName());
				}
			}
			if(obj.getLevel()<3) {
				obj.setAddChapterBtn(1);
			}else {
				obj.setAddChapterBtn(0);
			}


			Wrapper<ChapterDO> wrapperChildrens  = new EntityWrapper<ChapterDO>().orderBy("id", false);
			wrapperChildrens.eq("parent_id", obj.getId());

			List<ChapterDO> ChapterChildrens = chapterService.selectList(wrapperChildrens);
			if(ChapterChildrens.size()>0){
				obj.setIsHaveChildren(1);
			}else {
				obj.setIsHaveChildren(0);
			}
			list.add(obj);
		}

		page.setRecords(list);


		return Result.ok(page);

	}



	@GetMapping("/add")
	@RequiresPermissions("jzweb:chapter:add")
	String add(String coursesId,String parentId,Model model){
		model.addAttribute("coursesId",coursesId);
		if(parentId!=null) {
			model.addAttribute("parentId", parentId);
		}else {
			model.addAttribute("parentId", "");
		}
		System.out.println("coursesId:"+coursesId);
		System.out.println("parentId:"+parentId);
		CoursesDO coursesDO = coursesService.selectById(Long.valueOf(coursesId));
		if(coursesDO!=null){
			System.out.println(coursesDO.getName());
		}
		model.addAttribute("coursesName",coursesDO.getName());
		model.addAttribute("proId",coursesDO.getProfileId());
		return "jzweb/chapter/add";
	}

	@GetMapping("/edit/{id}")
	@RequiresPermissions("jzweb:chapter:edit")
	String edit(@PathVariable("id") Long id,Model model){
		ChapterDO chapter = chapterService.selectById(id);
		model.addAttribute("chapter", chapter);
		CoursesDO coursesDO = coursesService.selectById(chapter.getCoursesId());
		model.addAttribute("coursesName",coursesDO.getName());
		model.addAttribute("proId",coursesDO.getProfileId());
		return "jzweb/chapter/edit";
	}

	@GetMapping("/info/{id}")
	@RequiresPermissions("jzweb:chapter:chapter")
	String info(@PathVariable("id") Long id, Model model) {
		ChapterDO chapter = chapterService.selectById(id);
		//知识点名称
		if (chapter.getKpointsId() != null) {
			KnowledgePointsDO knowledgePointsDO = knowledgePointsService.selectById(chapter.getKpointsId());
			if (knowledgePointsDO != null) {
				chapter.setKpointsName(knowledgePointsDO.getName());
			}
		}
		//课程名称
		if (chapter.getCoursesId() != null) {
			CoursesDO coursesDO = coursesService.selectById(chapter.getCoursesId());
			if (coursesDO != null) {
				chapter.setCoursesName(coursesDO.getName());
			}
		}
		model.addAttribute("chapter", chapter);
		return "jzweb/chapter/info";
	}

	@Log("添加章节表")
	@ResponseBody
	@PostMapping("/save")
	@RequiresPermissions("jzweb:chapter:add")
	public Result<String> save( ChapterDO chapter){
		if(chapter.getParentId()!=null && !chapter.getParentId().equals("")){
			ChapterDO parentChapter = this.chapterService.selectById(chapter.getParentId());
			if(parentChapter.getLevel()!=null){
				chapter.setLevel(parentChapter.getLevel()+1);
			}else {
				chapter.setLevel(1);
			}
			chapter.setIsRoot(0);
		}
		chapterService.insert(chapter);
		return Result.ok();
	}

	@Log("修改章节表")
	@ResponseBody
	@RequestMapping("/update")
	@RequiresPermissions("jzweb:chapter:edit")
	public Result<String>  update( ChapterDO chapter){
		boolean update = chapterService.updateById(chapter);
		return update ? Result.ok() : Result.fail();
	}

	@Log("删除章节表")
	@PostMapping( "/remove")
	@ResponseBody
	@RequiresPermissions("jzweb:chapter:remove")
	public Result<String>  remove( Long id){
		chapterService.deleteById(id);
		return Result.ok();
	}

	@Log("批量删除章节表")
	@PostMapping( "/batchRemove")
	@ResponseBody
	@RequiresPermissions("jzweb:chapter:batchRemove")
	public Result<String>  remove(@RequestParam("ids[]") Long[] ids){
		chapterService.deleteBatchIds(Arrays.asList(ids));
		return Result.ok();
	}

}
